package com.hzqy.web.ac.itf;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hzqy.commons.utils.ConstantUtils;
import com.hzqy.service.ift.LoginServiceBean;

@Controller
@RequestMapping("/itf")
public class LoginAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());

	@Resource
	private LoginServiceBean loginServiceBean;
	
	@RequestMapping(value = "/login")
	public Object login(ModelMap map, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
		long t1 = System.currentTimeMillis();
		//一点登录
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			LOG.debug("(" + t1 + ") 一点登录 参数:" + request.getQueryString());
			resultMap = loginServiceBean.login(request.getParameterMap());
			LOG.debug("(" + t1 + ") 一点登录结果:" + resultMap + "登录耗时:" + (System.currentTimeMillis() - t1) + "ms");
			if((Boolean)resultMap.get("success")){
				//登录成功
				session.setAttribute(ConstantUtils.SESSION_ATTRIBUTE, resultMap.get(ConstantUtils.SESSION_ATTRIBUTE));
				session.setAttribute(ConstantUtils.SESSION_RESOURCE, resultMap.get(ConstantUtils.SESSION_RESOURCE));
				session.setAttribute(ConstantUtils.SESSION_USER, resultMap.get(ConstantUtils.SESSION_USER));
				return "redirect:/main.do";
			}else{
				//登录失败
				if((Boolean)resultMap.get("exist_errorurl")){
					String errorUrl = String.valueOf(resultMap.get("errorurl"));
					return "redirect:" + errorUrl;
				}else{
					//如果登录失败 并且没有errorurl
					return "redirect:/login.do";
				}
			}
		} catch (Exception e) {
			LOG.error("", e);
			return "redirect:/login.do";
		}
		//return "redirect:/login.do";
	}
	
}
